import {
  CHANGE_INPUT_VALUE,
  ADD_ITEM,
  DELETE_TODO_ITEM,
  INI_LIST
} from './actionCreators'

const defaultState = {
  inputValue: 'Hello',
  list: [1, 2, 3]
}

export default (state = defaultState, action) => {
  switch (action.type) {
    case CHANGE_INPUT_VALUE:
      return {
        ...state,
        inputValue: action.value
      }
    case ADD_ITEM: {
      if (state.inputValue !== '') {
        return {
          inputValue: '',
          list: [...state.list, state.inputValue]
        }
      }
      return {
        ...state,
        list: [...state.list]
      }
    }
    case DELETE_TODO_ITEM: {
      // const newState = JSON.parse(JSON.stringify(state))
      // newState.list.splice(action.index,1)
      // return newState
      const newState = [...state.list]
      newState.splice(action.index, 1)
      return {
        ...state,
        list: newState
      }
    }
    case INI_LIST:
      return {
        ...state,
        list: action.list
      }
    default:
      return state
  }
}
